import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import { Base64Helper } from "../../src/engine/utils/Base64Helper";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class AccountDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_account;
    public static Tb_Name: string = "t_accounts";

    private static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_account2
        // } 
        // else if(Config.environment == Config.EnvironmentType.xj){
        //     return DbConfig.Db_Name.db_account2
        // }
        // else if(Config.environment == Config.EnvironmentType.Jk){
        //     return DbConfig.Db_Name.db_account3
        // }
        // else {
            return DbConfig.Db_Name.db_account
        // }
    }

    public static async is_account_exist(account: string): Promise<any> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select account from " + this.getDbName() + "." + this.Tb_Name + " where `account`=?;"
        let sqlParams = [account];
        let results = await client.queryPromise(sql, sqlParams);
        // Logger.log("is_account_exist", results)
        if (results && results.length > 0) {
            return true
        }
        else {
            return false;
        }
    }

    public static async get_account_info(account: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select * from " + this.getDbName() + "." + this.Tb_Name + " where `account`=?;"
        let sqlParams = [account];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            results[0]["nick_name"] = Base64Helper.decode(results[0]["nick_name"])
            Logger.log("get_account_info", results)
            return results[0]
        } else {
            return null
        }
    }

    public static async create_account(account: string, password: string, account_type: number, token: string, status: number,
        ip: string, nick_name: string, channel: number, os: string, phone_type: string, bundle_name: string,
        system_version: string, sex: number = 1, headimgurl: string = ""): Promise<boolean> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        nick_name = Base64Helper.encode(nick_name)
        // let sql: string = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `account`=?;"
        let sql: string = 'insert INTO t_accounts(account,password,account_type, token, status, ip, nick_name, channel,os,phone_type, bundle_name, system_version, create_time, last_login_time, sex, headimgurl) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ;';
        let create_time: string = DateUtil.formatDateStr(DateUtil.now())
        let sqlParams = [account, password, account_type, token, status, ip, nick_name, channel, os, phone_type, bundle_name, system_version, create_time, create_time, sex, headimgurl];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("create_account", results)
        if (results && results["affectedRows"] > 0) {
            return true
        } else {
            return false
        }
    }

    public static async update_login(account: string, token: string, ip: string, nick_name: string, channel: number,
        os: string, phone_type: string, bundle_name: string, system_version: string, sex: number = 1, headimgurl: string = "") {
        let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `token`=?, `last_login_time`=?, `ip`=?, `nick_name`=?, `channel`=?, `os`=?, `phone_type`=?, `bundle_name`=?, `system_version`=?, `sex`=?, `headimgurl`=?   where `account`=?";
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        nick_name = Base64Helper.encode(nick_name)
        let last_login_time: string = DateUtil.formatDateStr(DateUtil.now())
        let sqlParams = [token, last_login_time, ip, nick_name, channel, os, phone_type, bundle_name, system_version,
            sex, headimgurl, account];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("update_login=", sql, sqlParams, results)
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async reset_pass(phone: string, password: string) {
        let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `password`=?  where `phone`=?";
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sqlParams = [password, phone];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("reset_pass=", sql, sqlParams, results)
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async bindPhone(account: string, phone: string, pass: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `phone`=?, `password`=?  where `account`=?";
        let sqlParams = [phone, pass, account];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("bindPhone=", sql, sqlParams, results)
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async getAccountByPhone(phone: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select * from " + this.getDbName() + "." + this.Tb_Name + " where `phone`=?;"
        let sqlParams = [phone];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            results[0]["nick_name"] = Base64Helper.decode(results[0]["nick_name"])
            Logger.log("getAccountByPhone", results)
            return results[0]
        } else {
            return null
        }
    }





}